// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.mediapipe.tasks.vision.holisticlandmarker;

import com.google.auto.value.AutoValue;
import com.google.mediapipe.formats.proto.LandmarkProto.LandmarkList;
import com.google.mediapipe.formats.proto.LandmarkProto.NormalizedLandmarkList;
import com.google.mediapipe.formats.proto.ClassificationProto.ClassificationList;
import com.google.mediapipe.framework.image.MPImage;
import com.google.mediapipe.tasks.components.containers.Category;
import com.google.mediapipe.tasks.components.containers.Landmark;
import com.google.mediapipe.tasks.components.containers.NormalizedLandmark;
import com.google.mediapipe.tasks.core.TaskResult;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/** Represents the holistic landmarks detection results generated by {@link HolisticLandmarker}. */
@AutoValue
@SuppressWarnings("AutoValueImmutableFields") // 3P API that does not depend on Guava
public abstract class HolisticLandmarkerResult implements TaskResult {

  /**
   * Creates a {@link HolisticLandmarkerResult} instance from a list of proto and image inputs.
   *
   * @param faceLandmarkListProto the detected face landmarks in normalized image coordinates
   * @param faceBlendshapeProtos the optional face blendshapes result
   * @param poseLandmarkListProtos the detected pose landmarks in normalized image coordinates
   * @param poseWorldLandmarkListProto the pose landmarks in world coordinates of detected poses
   * @param segmentationMask the segmentation mask for the detected pose
   * @param leftHandLandmarkListProto left hand landmarks of detected left hands
   * @param leftHandWorldLandmarkListProto left hand landmarks in world coordinates of detected left
   *     hands
   * @param rightHandLandmarkListProto right hand landmarks of detected left hands.
   * @param rightHandWorldLandmarkListProto right hand landmarks in world coordinates of detected
   *     left hands
   * @param timestampMs the time in milliseconds this result was created at
   * @throws IllegalArgumentException if there was an error creating {@link
   *     HolisticLandmarkerResult}
   */
  static HolisticLandmarkerResult create(
      NormalizedLandmarkList faceLandmarkListProto,
      Optional<ClassificationList> faceBlendshapeProtos,
      NormalizedLandmarkList poseLandmarkListProtos,
      LandmarkList poseWorldLandmarkListProto,
      Optional<MPImage> segmentationMask,
      NormalizedLandmarkList leftHandLandmarkListProto,
      LandmarkList leftHandWorldLandmarkListProto,
      NormalizedLandmarkList rightHandLandmarkListProto,
      LandmarkList rightHandWorldLandmarkListProto,
      long timestampMs) {
    List<NormalizedLandmark> faceLandmarks =
        NormalizedLandmark.createListFromProto(faceLandmarkListProto);
    Optional<List<Category>> faceBlendshapes =
        faceBlendshapeProtos.map(Category::createListFromProto);
    List<NormalizedLandmark> poseLandmarks =
        NormalizedLandmark.createListFromProto(poseLandmarkListProtos);
    List<Landmark> poseWorldLandmarks = Landmark.createListFromProto(poseWorldLandmarkListProto);
    List<NormalizedLandmark> leftHandLandmarks =
        NormalizedLandmark.createListFromProto(leftHandLandmarkListProto);
    List<Landmark> leftHandWorldLandmarks =
        Landmark.createListFromProto(leftHandWorldLandmarkListProto);
    List<NormalizedLandmark> rightHandLandmarks =
        NormalizedLandmark.createListFromProto(rightHandLandmarkListProto);
    List<Landmark> rightHandWorldLandmarks =
        Landmark.createListFromProto(rightHandWorldLandmarkListProto);

    return new AutoValue_HolisticLandmarkerResult(
        timestampMs,
        Collections.unmodifiableList(faceLandmarks),
        faceBlendshapes,
        Collections.unmodifiableList(poseLandmarks),
        Collections.unmodifiableList(poseWorldLandmarks),
        segmentationMask,
        Collections.unmodifiableList(leftHandLandmarks),
        Collections.unmodifiableList(leftHandWorldLandmarks),
        Collections.unmodifiableList(rightHandLandmarks),
        Collections.unmodifiableList(rightHandWorldLandmarks));
  }

  /**
   * Creates an empty {@link HolisticLandmarkerResult} instance.
   *
   * @param timestampMs the time in milliseconds this result was created at
   */
  static HolisticLandmarkerResult createEmpty(long timestampMs) {
    return new AutoValue_HolisticLandmarkerResult(
        timestampMs,
        Collections.emptyList(),
        Optional.empty(),
        Collections.emptyList(),
        Collections.emptyList(),
        Optional.empty(),
        Collections.emptyList(),
        Collections.emptyList(),
        Collections.emptyList(),
        Collections.emptyList());
  }

  @Override
  public abstract long timestampMs();

  /** Detected face landmarks in normalized image coordinates. */
  public abstract List<NormalizedLandmark> faceLandmarks();

  /** Optional face blendshapes. */
  public abstract Optional<List<Category>> faceBlendshapes();

  /** Detected pose landmarks in normalized image coordinates. */
  public abstract List<NormalizedLandmark> poseLandmarks();

  /** Pose landmarks in world coordinates of the detected pose. */
  public abstract List<Landmark> poseWorldLandmarks();

  /** Segmentation mask for the detected pose. */
  public abstract Optional<MPImage> segmentationMask();

  /** Hand landmarks of detected left hands. */
  public abstract List<NormalizedLandmark> leftHandLandmarks();

  /** Hnd landmarks in world coordinates of detected left hands. */
  public abstract List<Landmark> leftHandWorldLandmarks();

  /** Hand landmarks of detected right hands. */
  public abstract List<NormalizedLandmark> rightHandLandmarks();

  /** Hand landmarks in world coordinates of detected right hands. */
  public abstract List<Landmark> rightHandWorldLandmarks();
}
